<template>
    <div class="qtDepreciation">
        <qtHeader fatherTitle="资产折旧" fatherCreate="创建新的" @tell='getName'></qtHeader>
        <el-row>
      <el-col :span="16">
        <el-table :data="tableData" stripe style="width: 100%">
          <el-table-column prop="name" label="名称" width="400">
          </el-table-column>
          <el-table-column  label="保修" width="400">
            <template slot-scope="scope">
              {{scope.row.guarantee}}个月
            </template>
          </el-table-column>
          <el-table-column label="操作" >
            <template slot-scope="scope" >
              <el-button size="mini" type="warning" @click="handleEdit(scope.row)">
                <i class="el-icon-edit"></i>
              </el-button>
              <el-button size="mini" type="danger" @click="handleDelete(scope.row)">
                <i class="el-icon-delete"></i>
              </el-button>
            </template>
          </el-table-column>
        </el-table>
      </el-col>
      <el-col :span="8"></el-col>
    </el-row>
     <!-- 添加弹框 -->
    <el-dialog  :visible.sync="dialogFormVisible" width="800px"  center custom-class="dialogClass" @close="cancel">
      <el-form :model="form" ref="dataform">
        <el-form-item label="名称" :label-width="formLabelWidth" prop="name">
          <el-input v-model="form.name" autocomplete="off"></el-input>
        </el-form-item>
         <el-form-item label="保修" :label-width="formLabelWidth" prop="guarantee">
          <el-input v-model="form.guarantee" autocomplete="off">
            <template slot="suffix">个月</template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </div>
    </el-dialog>
    </div>
</template>

<script>
import qtHeader from '../components/header'
import { getDepreciation, addDepreciation, delDepreciation, checkDepreciation } from '@/api/index.js'
export default {
  components: {
    qtHeader
  },
  data () {
    return {
      tableData: [],
      dialogFormVisible: false,
      formLabelWidth: '200px',
      row: '',
      assetsId: '',
      form: {
        name: '',
        guarantee: ''
      }
    }
  },
  created () {
    this.getDepreciation()
  },
  methods: {
    getDepreciation () {
      getDepreciation().then(res => {
      // console.log(res)
        if (res.data.code === 200) {
          this.tableData = res.data.data
        }
      })
    },
    getName (name) {
      this.dialogFormVisible = name
    },
    handleEdit (row) {
      this.getName(name)
      this.row = row
      console.log(row)
      this.assetsId = row.id
      this.form.guarantee = row.guarantee
      this.form.name = row.name
    },
    handleDelete (row) {
      this.$confirm('是否删除资产?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        delDepreciation(row.id).then(res => {
          if (res.data.code === 200) {
            this.$message.success(res.data.msg)
          } else {
            this.$message.error(res.data.msg)
          }
          // 判断当删除的数据为当前页中的最后一条数据，刷新页面的时候需要把当前页码数减1
          if (this.total / this.pageSize - parseInt(this.total / this.pageSize) <= 0.1) {
            this.page = this.page - 1
          }
          this.getDepreciation()
        })
      }).catch(() => {
        this.$message.info('已取消删除')
      })
    },
    cancel () {
      this.assetsId = ''
      this.$refs.dataform.resetFields()
      for (var key in this.form) {
        this.form[key] = ''
      }
      this.dialogFormVisible = false
    },
    confirm () {
      var obj = {
        name: this.form.name,
        guarantee: this.form.guarantee
      }
      if (this.assetsId) {
        obj.id = this.assetsId
        if (!isNaN(this.form.guarantee)) {
          obj.guarantee = this.form.guarantee
        } else {
          obj.guarantee = this.row.guarantee
        }
        if (this.row.name !== this.form.name) {
          obj.name = this.form.name
        } else {
          obj.name = this.row.name
        }
        checkDepreciation(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success('修改折旧成功')
            this.getDepreciation()
          } else {
            this.$message.error('修改折旧失败')
          }
        })
      } else {
        addDepreciation(obj).then(res => {
          if (res.data.code === 200) {
            this.$message.success('新建折旧成功')
            this.getDepreciation()
          } else {
            this.$message.error('新建折旧失败')
          }
        })
      }

      this.dialogFormVisible = false
    }
  }
}
</script>

<style lang="scss" scoped>
.qtDepreciation{
  padding: 10px;
}
.el-input.el-input--suffix{
  width: 350px;
}
</style>
